<?php
abstract class BaseRequestHandler extends Object implements ISingleton
{
	public $url;
    public $subUrl;
	public $action;
	public $params;
    public $controller;
    public $timestart;
    public $elapsedTime;
    public $showDebug = true;

    protected $defaultAction = 'Index';

	protected function BaseRequestHandler($timestart)
	{
		$this->timestart = $timestart;
		$this->url = self::GetGet('__pr');
        $this->subUrl = self::Get('__cr');
	}

    protected function LoadController()
	{
		$urls = explode(FS, $this->url);
        $controllerName = String::Pascalize($urls[0]);
        if (!$controllerName) $controllerName = AppConfig::GetInstance()->DEFAULT_CONTROLLER;

        $controllerName .= 'Controller';
	    $file = $controllerName.'.php';
	    $this->action = $this->GetAction($urls);

		if (!including($file)) return false;

        $this->controller = new $controllerName();
        if (!method_exists($this->controller, $this->action))
		{
			if (method_exists($this->controller, $this->defaultAction))
			{
				$this->action = $this->defaultAction;
		        $this->params = array_slice($urls, 1);
			}
			else return false;
		}
		else
	    	$this->params = array_slice($urls, 2);

        $this->controller->SetAction($this->action);
        $this->controller->SetParams($this->params);
        return true;
	}

    protected function LoadPage()
    {
        return false;
    }

    public function Process()
	{
		if ($this->LoadPage() || $this->LoadController())
        {
            $out = $this->controller->RenderPage();
            $out .= $this->RenderMessage();
            $this->elapsedTime = Timer::StopTimer($this->timestart);
            $out .= $this->RenderDebugInfo();
            MysqlAdapter::CloseAll();
            return $out;
        }
        else
        {
            $this->PageNotFound();
        }
    }

    protected function RenderMessage()
    {
        return '<!-- msg-flash -->'.MessageControl::Flash();
    }

    protected function RenderDebugInfo()
    {
		if ((AppConfig::GetInstance()->IsDebugMode()) && $this->showDebug)
            return LogControl::GetInstance()->Render();
    }

    protected function GetAction($urls)
    {
        if (count($urls)<2) return $this->defaultAction;
        $tmp = str_replace('-', '_', $urls[1]);
        return String::Pascalize($tmp);
    }

    public function PageNotFound()
    {
        header($_SERVER["SERVER_PROTOCOL"].' 404 Not Found');
        header('Status: 404 Not Found');
        echo 'Page not found!';
        exit_app();
    }

    public static function Redirect($url, $exit = true)
    {
        header("location:$url");
        if ($exit) exit_app();
    }

    public static function Back($exit = true)
    {
        self::Redirect(is_set($_SERVER['HTTP_REFERER'], AppConfig::GetInstance()->URL), $exit);
    }

    public static function Get($name)
    {
        return self::IsExisted($name) ? $_REQUEST[$name] : null;
    }

    public static function GetPost($name)
    {
        return self::IsPost($name) ? $_POST[$name] : null;
    }

    public static function GetGet($name)
    {
        return self::IsGet($name) ? $_GET[$name] : null;
    }

    public static function IsExisted($name)
    {
        return isset($_REQUEST[$name]);
    }

    public static function IsPost($name)
    {
        return isset($_POST[$name]);
    }

    public static function IsGet($name)
    {
        return isset($_GET[$name]);
    }

    public static function GetFilter()
    {
        $filter = self::Get('filter');
        return $filter ? $filter : array();
    }

    public static function GetFilterValue($key)
    {
        $filter = self::GetFilter();
        return isset($filter[$key]) ? $filter[$key] : null;
    }

    public static function Set($key, $value)
    {
        $_REQUEST[$key] = $value;
    }
}
?>
